// pages/cart/index.js
import {request,fail,ok} from "../../util"
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList:[],
     //购物车是否全选
     cartAllChoose:false,
     //购物车选择的项，存的cartId，但是string型
     cartChoose:[],
     //统计价格
     cartChoosePrice:0.0
  },
  setCartAmountByOne(e){
    let amount= parseInt(e.currentTarget.dataset.num)+ 
    parseInt((e.currentTarget.dataset.a))
    amount= amount<=0?1:amount;
    let cartId= parseInt(e.currentTarget.dataset.id)
    this.reqSetCartAmount(amount,cartId)
  },
  reqSetCartAmount(amount,cartId){
    request({
      url:"/cart/sku/set",
      method:"POST",
      data:{
        amount,
        cartId
      }
    }).then(res=>{
      //this.getCartCount()
      let cartList= this.data.cartList
      for(let i=0;i<cartList.length;i++){
        if(cartList[i].cartId===cartId){
          cartList[i]["amount"]= amount
          break
        }
      }
      this.setData({cartList})
      this.refreshPriceAndCheckBox()
    })
  },
  deleteCart(e){
    let cartId= parseInt(e.currentTarget.dataset.id)

    Dialog.confirm({
      title: '从购物车删除',
      message: '确认删除?',
    })
      .then(() => {
        request({
          url:"/cart/delete?cartId="+cartId,
          method:"POST"
        }).then(res=>{
          this.getCartList()
          //this.getCartCount()
          //删除购物车选择项
          let cartChoose=this.data.cartChoose
          for(let i=0;i<cartChoose.length;i++){
            if(parseInt(cartChoose[i])=== cartId){
              cartChoose.splice(i,1)
              break
            }
          }
          this.setData({cartChoose})
        })
      })
      .catch(() => {
        // on cancel
      });

    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCartList()
  },
  getCartList(){
    request({
      url:"/cart/list?pageShow=false"
    }).then(res=>{
      this.setData({cartList:res.data.list})
      this.refreshPriceAndCheckBox()
    })
  },
  refreshPriceAndCheckBox(){
    let cartChoose= this.data.cartChoose
    let cartList= this.data.cartList
    this.setData({cartAllChoose:cartChoose.length===cartList.length  })
    let cartChoosePrice=0.0
    cartChoose.forEach(e=>{
      let cart=  cartList.find( c=> c.cartId=== parseInt(e) )
      cartChoosePrice+= (cart.price* cart.amount)
    })
    this.setData({cartChoosePrice})
  },
  cartChooseChange(e){
    this.setData({
      cartChoose: e.detail,
    });
    this.refreshPriceAndCheckBox()
  },
  onChangeAllChoose(e){
    this.setData({
      cartAllChoose: e.detail,
    });
    let cartList= this.data.cartList
    let cartChoose=[]
    if(this.data.cartAllChoose){
      for(let i=0;i<cartList.length;i++){
        cartChoose.push(cartList[i].cartId.toString())
      }
    }
    
    this.setData({cartChoose})
    this.refreshPriceAndCheckBox()
  },
  setCartAmount(e){
    let amount= !e.detail.value? parseInt((e.currentTarget.dataset.a)):
     parseInt(e.detail.value)
    amount= amount<=0?1:amount;
    let cartId= parseInt(e.currentTarget.dataset.id)
    this.reqSetCartAmount(amount,cartId)
  },
  toPrepay(){
    if(!this.data.cartChoose||this.data.cartChoose.length===0){
      fail("请选择商品")
      return
    }
  
    wx.navigateTo({
      url: '/pages/prepay/index?cartIds='+ this.data.cartChoose.join(","),
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})